/**
 *   Copyright (C) 2011 - Casey Burkhardt
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
**/

package com.tightometer.android;

import com.tightometer.android.R;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceChangeListener;

public class TightometerPreferencesActivity extends PreferenceActivity {
    public static final String KEY_TIGHTOMETER_INSTANCE_URL = "tightometer_instance_url";
    
    public static final String KEY_TIGHTOMETER_INSTANCE_PASSWORD = "tightometer_instance_password";

    public static final String KEY_SOUND_ENABLED = "tightometer_sounds_enabled";
    
    private TightometerPreferencesActivity self;
    
    private SharedPreferences mSettings;

    private EditTextPreference mInstanceUrlPreference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        self = this;
        
        // Load the XML preferences file
        addPreferencesFromResource(R.xml.preferences);

        // Get the System SharedPreferences Reference
        mSettings = PreferenceManager.getDefaultSharedPreferences(this);
        
        // Get a reference to the preferences
        mInstanceUrlPreference = (EditTextPreference) getPreferenceScreen().findPreference(KEY_TIGHTOMETER_INSTANCE_URL);
        
        mInstanceUrlPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
            
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                String instanceURL = (String) newValue;
                instanceURL = instanceURL.trim();
                
                // If the user didn't include a protocol, default to "http://"
                // This implementation allows the user to enter "https://" without overwriting.
                if (!instanceURL.startsWith(getString(R.string.utility_http_match_prefix))) {
                    instanceURL = getString(R.string.utility_http_prepend) + instanceURL;
                }
                
                // Remove trailing slashes, as they're appended based on operation.
                if (instanceURL.endsWith(getString(R.string.utility_url_trailing_slash))) {
                    instanceURL = instanceURL.substring(0, instanceURL.length() - 1);
                }

                SharedPreferences.Editor editor = mSettings.edit();
                editor.putString(KEY_TIGHTOMETER_INSTANCE_URL, instanceURL);
                editor.commit();
                finish();
                startActivity(new Intent(self, TightometerPreferencesActivity.class));
                return false;
            }
        });
    }
}
